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

Commit

Permalink
Changed the compile-time switch for DH support into a runtime check f…
Browse files Browse the repository at this point in the history
…or the supporting assembling.

Fixes #87
  • Loading branch information
AArnott committed Mar 11, 2012
1 parent 92ee03c commit 34da8f9
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 47 deletions.
Expand Up @@ -31,7 +31,7 @@
<Compile Include="OpenId\Provider\Extensions\UI\UIRequestTools.cs" />
<Compile Include="OpenId\Provider\HmacShaAssociationProvider.cs" />
<Compile Include="OpenId\Messages\AssociateDiffieHellmanProviderRequest.cs" />
<Compile Include="OpenId\Messages\AssociateDiffieHellmanProviderResponse.cs" Condition=" '$(ExcludeDiffieHellman)' != 'true' " />
<Compile Include="OpenId\Messages\AssociateDiffieHellmanProviderResponse.cs" />
<Compile Include="OpenId\Messages\IAssociateRequestProvider.cs" />
<Compile Include="OpenId\Messages\AssociateRequestProviderTools.cs" />
<Compile Include="OpenId\Messages\IAssociateSuccessfulResponseProvider.cs" />
Expand Down Expand Up @@ -67,7 +67,7 @@
<Project>{3896A32A-E876-4C23-B9B8-78E17D134CD3}</Project>
<Name>DotNetOpenAuth.OpenId</Name>
</ProjectReference>
<ProjectReference Include="..\Org.Mentalis.Security.Cryptography\Org.Mentalis.Security.Cryptography.csproj" Condition=" '$(ExcludeDiffieHellman)' != 'true' ">
<ProjectReference Include="..\Org.Mentalis.Security.Cryptography\Org.Mentalis.Security.Cryptography.csproj" >
<Project>{26DC877F-5987-48DD-9DDB-E62F2DE0E150}</Project>
<Name>Org.Mentalis.Security.Cryptography</Name>
</ProjectReference>
Expand Down
Expand Up @@ -31,13 +31,9 @@ internal AssociateDiffieHellmanProviderRequest(Version version, Uri providerEndp
/// The appropriate association response message.
/// </returns>
public IProtocolMessage CreateResponseCore() {
#if !ExcludeDiffieHellman
var response = new AssociateDiffieHellmanProviderResponse(this.Version, this);
response.AssociationType = this.AssociationType;
return response;
#else
throw new NotSupportedException();
#endif
}
}
}
Expand Up @@ -34,7 +34,7 @@
<Compile Include="OpenId\Interop\AuthenticationResponseShim.cs" />
<Compile Include="OpenId\Interop\ClaimsResponseShim.cs" />
<Compile Include="OpenId\Interop\OpenIdRelyingPartyShim.cs" />
<Compile Include="OpenId\Messages\AssociateDiffieHellmanRelyingPartyResponse.cs" Condition=" '$(ExcludeDiffieHellman)' != 'true' " />
<Compile Include="OpenId\Messages\AssociateDiffieHellmanRelyingPartyResponse.cs" />
<Compile Include="OpenId\Messages\AssociateRequestRelyingParty.cs" />
<Compile Include="OpenId\Messages\IAssociateSuccessfulResponseRelyingPartyContract.cs" />
<Compile Include="OpenId\Messages\IAssociateSuccessfulResponseRelyingParty.cs" />
Expand Down Expand Up @@ -71,7 +71,7 @@
<Project>{3896A32A-E876-4C23-B9B8-78E17D134CD3}</Project>
<Name>DotNetOpenAuth.OpenId</Name>
</ProjectReference>
<ProjectReference Include="..\Org.Mentalis.Security.Cryptography\Org.Mentalis.Security.Cryptography.csproj" Condition=" '$(ExcludeDiffieHellman)' != 'true' ">
<ProjectReference Include="..\Org.Mentalis.Security.Cryptography\Org.Mentalis.Security.Cryptography.csproj">
<Project>{26DC877F-5987-48DD-9DDB-E62F2DE0E150}</Project>
<Name>Org.Mentalis.Security.Cryptography</Name>
</ProjectReference>
Expand Down
Expand Up @@ -95,12 +95,12 @@ internal class OpenIdRelyingPartyMessageFactory : IMessageFactory {
// This is a special recognized error case that we create a special message for.
message = new AssociateUnsuccessfulResponse(protocol.Version, associateRequest);
} else if (message == null) {
#if !ExcludeDiffieHellman
var associateDiffieHellmanRequest = request as AssociateDiffieHellmanRequest;
if (associateDiffieHellmanRequest != null) {
message = new AssociateDiffieHellmanRelyingPartyResponse(protocol.Version, associateDiffieHellmanRequest);
if (OpenIdUtilities.IsDiffieHellmanPresent) {
var associateDiffieHellmanRequest = request as AssociateDiffieHellmanRequest;
if (associateDiffieHellmanRequest != null) {
message = new AssociateDiffieHellmanRelyingPartyResponse(protocol.Version, associateDiffieHellmanRequest);
}
}
#endif

var associateUnencryptedRequest = request as AssociateUnencryptedRequest;
if (associateUnencryptedRequest != null) {
Expand Down
Expand Up @@ -68,15 +68,15 @@ internal static class AssociateRequestRelyingParty {
associateRequest.AssociationType = associationType;
return associateRequest;
} else {
#if !ExcludeDiffieHellman
var associateRequest = new AssociateDiffieHellmanRequest(provider.Version, provider.Uri);
associateRequest.AssociationType = associationType;
associateRequest.SessionType = sessionType;
associateRequest.InitializeRequest();
return associateRequest;
#else
return null;
#endif
if (OpenIdUtilities.IsDiffieHellmanPresent) {
var associateRequest = new AssociateDiffieHellmanRequest(provider.Version, provider.Uri);
associateRequest.AssociationType = associationType;
associateRequest.SessionType = sessionType;
associateRequest.InitializeRequest();
return associateRequest;
} else {
return null;
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/DotNetOpenAuth.OpenId/DotNetOpenAuth.OpenId.csproj
Expand Up @@ -123,11 +123,11 @@
<Compile Include="OpenId\ProviderEndpointDescription.cs" />
<Compile Include="OpenId\Realm.cs" />
<Compile Include="OpenId\RelyingPartyEndpointDescription.cs" />
<Compile Include="OpenId\DiffieHellmanUtilities.cs" Condition=" '$(ExcludeDiffieHellman)' != 'true' " />
<Compile Include="OpenId\DiffieHellmanUtilities.cs" />
<Compile Include="OpenId\HmacShaAssociation.cs" />
<Compile Include="OpenId\Messages\AssociateUnencryptedRequest.cs" />
<Compile Include="OpenId\Messages\AssociateDiffieHellmanRequest.cs" />
<Compile Include="OpenId\Messages\AssociateDiffieHellmanResponse.cs" Condition=" '$(ExcludeDiffieHellman)' != 'true' " />
<Compile Include="OpenId\Messages\AssociateDiffieHellmanResponse.cs" />
<Compile Include="OpenId\Messages\AssociateRequest.cs" />
<Compile Include="OpenId\Messages\AssociateSuccessfulResponse.cs" />
<Compile Include="OpenId\Messages\AssociateUnencryptedResponse.cs" />
Expand Down Expand Up @@ -180,7 +180,7 @@
<Project>{60426312-6AE5-4835-8667-37EDEA670222}</Project>
<Name>DotNetOpenAuth.Core</Name>
</ProjectReference>
<ProjectReference Include="..\Org.Mentalis.Security.Cryptography\Org.Mentalis.Security.Cryptography.csproj" Condition=" '$(ExcludeDiffieHellman)' != 'true' ">
<ProjectReference Include="..\Org.Mentalis.Security.Cryptography\Org.Mentalis.Security.Cryptography.csproj">
<Project>{26DC877F-5987-48DD-9DDB-E62F2DE0E150}</Project>
<Name>Org.Mentalis.Security.Cryptography</Name>
</ProjectReference>
Expand Down
28 changes: 15 additions & 13 deletions src/DotNetOpenAuth.OpenId/OpenId/HmacShaAssociation.cs
Expand Up @@ -140,11 +140,13 @@ private HmacShaAssociation(HmacSha typeIdentity, string handle, byte[] secret, T
hashSizeInBits < securityRequirements.MinimumHashBitLength) {
continue;
}
#if !ExcludeDiffieHellman
sessionType = DiffieHellmanUtilities.GetNameForSize(protocol, hashSizeInBits);
#else
sessionType = requireMatchingDHSessionType ? null : protocol.Args.SessionType.NoEncryption;
#endif

if (OpenIdUtilities.IsDiffieHellmanPresent) {
sessionType = DiffieHellmanUtilities.GetNameForSize(protocol, hashSizeInBits);
} else {
sessionType = requireMatchingDHSessionType ? null : protocol.Args.SessionType.NoEncryption;
}

if (requireMatchingDHSessionType && sessionType == null) {
continue;
}
Expand Down Expand Up @@ -178,14 +180,14 @@ private HmacShaAssociation(HmacSha typeIdentity, string handle, byte[] secret, T
return true;
}

#if !ExcludeDiffieHellman
// When there _is_ a DH session, it must match in hash length with the association type.
int associationSecretLengthInBytes = GetSecretLength(protocol, associationType);
int sessionHashLengthInBytes = DiffieHellmanUtilities.Lookup(protocol, sessionType).HashSize / 8;
return associationSecretLengthInBytes == sessionHashLengthInBytes;
#else
return false;
#endif
if (OpenIdUtilities.IsDiffieHellmanPresent) {
// When there _is_ a DH session, it must match in hash length with the association type.
int associationSecretLengthInBytes = GetSecretLength(protocol, associationType);
int sessionHashLengthInBytes = DiffieHellmanUtilities.Lookup(protocol, sessionType).HashSize / 8;
return associationSecretLengthInBytes == sessionHashLengthInBytes;
} else {
return false;
}
}

/// <summary>
Expand Down
Expand Up @@ -12,9 +12,7 @@ namespace DotNetOpenAuth.OpenId.Messages {
using System.Text;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.Messaging.Reflection;
#if !ExcludeDiffieHellman
using Org.Mentalis.Security.Cryptography;
#endif

/// <summary>
/// An OpenID direct request from Relying Party to Provider to initiate an association that uses Diffie-Hellman encryption.
Expand Down Expand Up @@ -78,31 +76,25 @@ internal AssociateDiffieHellmanRequest(Version version, Uri providerEndpoint)
[MessagePart("openid.dh_consumer_public", IsRequired = true, AllowEmpty = false)]
internal byte[] DiffieHellmanConsumerPublic { get; set; }

#if !ExcludeDiffieHellman
/// <summary>
/// Gets the Diffie-Hellman algorithm.
/// </summary>
/// <remarks>
/// This property is initialized with a call to <see cref="InitializeRequest"/>.
/// </remarks>
internal DiffieHellman Algorithm { get; private set; }
#endif

/// <summary>
/// Called by the Relying Party to initialize the Diffie-Hellman algorithm and consumer public key properties.
/// </summary>
internal void InitializeRequest() {
#if !ExcludeDiffieHellman
if (this.DiffieHellmanModulus == null || this.DiffieHellmanGen == null) {
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, OpenIdStrings.DiffieHellmanRequiredPropertiesNotSet, string.Join(", ", new string[] { "DiffieHellmanModulus", "DiffieHellmanGen" })));
}

this.Algorithm = new DiffieHellmanManaged(this.DiffieHellmanModulus ?? DefaultMod, this.DiffieHellmanGen ?? DefaultGen, DefaultX);
byte[] consumerPublicKeyExchange = this.Algorithm.CreateKeyExchange();
this.DiffieHellmanConsumerPublic = DiffieHellmanUtilities.EnsurePositive(consumerPublicKeyExchange);
#else
throw new NotSupportedException();
#endif
}
}
}
31 changes: 31 additions & 0 deletions src/DotNetOpenAuth.OpenId/OpenId/OpenIdUtilities.cs
Expand Up @@ -10,6 +10,7 @@ namespace DotNetOpenAuth.OpenId {
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
Expand All @@ -19,6 +20,7 @@ namespace DotNetOpenAuth.OpenId {
using DotNetOpenAuth.OpenId.ChannelElements;
using DotNetOpenAuth.OpenId.Extensions;
using DotNetOpenAuth.OpenId.Messages;
using Org.Mentalis.Security.Cryptography;

/// <summary>
/// A set of utilities especially useful to OpenID.
Expand All @@ -29,6 +31,31 @@ public static class OpenIdUtilities {
/// </summary>
internal const string CustomParameterPrefix = "dnoa.";

private static bool? diffieHellmanPresent;

internal static bool IsDiffieHellmanPresent {
get {
if (!diffieHellmanPresent.HasValue) {
try {
LoadDiffieHellmanTypes();
diffieHellmanPresent = true;
} catch (FileNotFoundException) {
diffieHellmanPresent = false;
} catch (TypeLoadException) {
diffieHellmanPresent = false;
}

if (diffieHellmanPresent.Value) {
Logger.OpenId.Info("Diffie-Hellman supporting assemblies found and loaded.");
} else {
Logger.OpenId.Warn("Diffie-Hellman supporting assemblies failed to load. Only associations with HTTPS OpenID Providers will be supported.");
}
}

return diffieHellmanPresent.Value;
}
}

/// <summary>
/// Creates a random association handle.
/// </summary>
Expand Down Expand Up @@ -169,5 +196,9 @@ public static class OpenIdUtilities {
ErrorUtilities.VerifyOperation(aggregator != null, OpenIdStrings.UnsupportedChannelConfiguration);
return aggregator.Factories;
}

private static void LoadDiffieHellmanTypes() {
var dhAssemblyType = typeof(DiffieHellmanManaged);
}
}
}
1 change: 0 additions & 1 deletion tools/DotNetOpenAuth.targets
Expand Up @@ -18,7 +18,6 @@
<CodeContractsInstallDir>$(ProjectRoot)tools\Contracts\</CodeContractsInstallDir>
<DefineConstants Condition=" '$(SignAssembly)' == 'true' ">$(DefineConstants);StrongNameSigned</DefineConstants>
<DefineConstants Condition=" '$(ClrVersion)' == '4' ">$(DefineConstants);CLR4</DefineConstants>
<DefineConstants Condition=" '$(ExcludeDiffieHellman)' == 'true' ">$(DefineConstants);ExcludeDiffieHellman</DefineConstants>
<AssemblySearchPaths>$(ProjectRoot)lib;$(AssemblySearchPaths)</AssemblySearchPaths>
<AssemblySearchPaths Condition="Exists('$(ProjectRoot)lib\net-$(TargetFrameworkVersion)')">$(ProjectRoot)lib\net-$(TargetFrameworkVersion);$(AssemblySearchPaths)</AssemblySearchPaths>
</PropertyGroup>
Expand Down

0 comments on commit 34da8f9

Please sign in to comment.