Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - abstract the certificate store #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 102 additions & 100 deletions CSharpSoapToolkit/CSharpSoapToolkit/CSharpSoapToolkit.csproj
Original file line number Diff line number Diff line change
@@ -1,101 +1,103 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{509FFBAE-003B-44B4-8119-B74F1DEA7FC1}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>CSharpSoapToolkit</RootNamespace>
<AssemblyName>CSharpSoapToolkit</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="BouncyCastle.Crypto, Version=1.9.0.0, Culture=neutral, PublicKeyToken=0e99375e54769942, processorArchitecture=MSIL">
<HintPath>..\packages\Portable.BouncyCastle.1.9.0\lib\net40\BouncyCastle.Crypto.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.IdentityModel" />
<Reference Include="System.Runtime.Caching" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CertificateCacheUtility.cs" />
<Compile Include="Connected Services\com.cybersource.stub\Reference.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Reference.svcmap</DependentUpon>
</Compile>
<Compile Include="InspectorBehavior.cs" />
<Compile Include="PropertiesUtility.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Sample.cs" />
<Compile Include="SecurityUtility.cs" />
<Compile Include="SoapEnvelopeUtility.cs" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Connected Services\" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="Connected Services\com.cybersource.stub\CSharpSoapToolkit.com.cybersource.stub.outputMessageOut.datasource">
<DependentUpon>Reference.svcmap</DependentUpon>
</None>
<None Include="Connected Services\com.cybersource.stub\CSharpSoapToolkit.com.cybersource.stub.ReplyMessage.datasource">
<DependentUpon>Reference.svcmap</DependentUpon>
</None>
<None Include="Connected Services\com.cybersource.stub\CyberSourceTransactionWS.wsdl" />
<None Include="Connected Services\com.cybersource.stub\CyberSourceTransaction_1.xsd">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<WCFMetadataStorage Include="Connected Services\com.cybersource.stub\" />
</ItemGroup>
<ItemGroup>
<None Include="Connected Services\com.cybersource.stub\configuration91.svcinfo" />
</ItemGroup>
<ItemGroup>
<None Include="Connected Services\com.cybersource.stub\configuration.svcinfo" />
</ItemGroup>
<ItemGroup>
<None Include="Connected Services\com.cybersource.stub\Reference.svcmap">
<Generator>WCF Proxy Generator</Generator>
<LastGenOutput>Reference.cs</LastGenOutput>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{509FFBAE-003B-44B4-8119-B74F1DEA7FC1}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>CSharpSoapToolkit</RootNamespace>
<AssemblyName>CSharpSoapToolkit</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="BouncyCastle.Crypto, Version=1.9.0.0, Culture=neutral, PublicKeyToken=0e99375e54769942, processorArchitecture=MSIL">
<HintPath>..\packages\Portable.BouncyCastle.1.9.0\lib\net40\BouncyCastle.Crypto.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.IdentityModel" />
<Reference Include="System.Runtime.Caching" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CertificateCacheUtility.cs" />
<Compile Include="Connected Services\com.cybersource.stub\Reference.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Reference.svcmap</DependentUpon>
</Compile>
<Compile Include="InspectorBehavior.cs" />
<Compile Include="ISecureCertificateStore.cs" />
<Compile Include="PropertiesUtility.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Sample.cs" />
<Compile Include="SecurityUtility.cs" />
<Compile Include="SoapEnvelopeUtility.cs" />
<Compile Include="ToolkitCertificateStore.cs" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Connected Services\" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="Connected Services\com.cybersource.stub\CSharpSoapToolkit.com.cybersource.stub.outputMessageOut.datasource">
<DependentUpon>Reference.svcmap</DependentUpon>
</None>
<None Include="Connected Services\com.cybersource.stub\CSharpSoapToolkit.com.cybersource.stub.ReplyMessage.datasource">
<DependentUpon>Reference.svcmap</DependentUpon>
</None>
<None Include="Connected Services\com.cybersource.stub\CyberSourceTransactionWS.wsdl" />
<None Include="Connected Services\com.cybersource.stub\CyberSourceTransaction_1.xsd">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<WCFMetadataStorage Include="Connected Services\com.cybersource.stub\" />
</ItemGroup>
<ItemGroup>
<None Include="Connected Services\com.cybersource.stub\configuration91.svcinfo" />
</ItemGroup>
<ItemGroup>
<None Include="Connected Services\com.cybersource.stub\configuration.svcinfo" />
</ItemGroup>
<ItemGroup>
<None Include="Connected Services\com.cybersource.stub\Reference.svcmap">
<Generator>WCF Proxy Generator</Generator>
<LastGenOutput>Reference.cs</LastGenOutput>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
17 changes: 17 additions & 0 deletions CSharpSoapToolkit/CSharpSoapToolkit/ISecureCertificateStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace CSharpSoapToolkit
{
/// <summary>
/// Ensure any custom implentations are secure
/// </summary>
public interface ISecureCertificateStore
{
X509Certificate2 MerchantCertificate { get; }
}
}
141 changes: 73 additions & 68 deletions CSharpSoapToolkit/CSharpSoapToolkit/InspectorBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,68 +1,73 @@
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace CSharpSoapToolkit
{
public class InspectorBehavior : IEndpointBehavior
{
public InspectorBehavior()
{
// not calling the base implementation
}

public void Validate(ServiceEndpoint endpoint)
{
// not calling the base implementation
}

public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
// not calling the base implementation
}

public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
// not calling the base implementation
}

public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add(new ClientInspector());
}
}

public class ClientInspector : IClientMessageInspector
{
public MessageHeader[] Headers { get; set; }

public ClientInspector(params MessageHeader[] headers)
{
Headers = headers;
}

public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
MessageBuffer buffer = request.CreateBufferedCopy(int.MaxValue);
Message copy = buffer.CreateMessage();

if (Headers != null)
{
for (int i = Headers.Length - 1; i >= 0; i--)
request.Headers.Insert(0, Headers[i]);
}

SoapEnvelopeUtility.AddSecurityElements(ref copy);

request = copy;

return null;
}

public void AfterReceiveReply(ref Message reply, object correlationState)
{
// not calling the base implementation
}
}
}
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace CSharpSoapToolkit
{
public class InspectorBehavior : IEndpointBehavior
{
private ISecureCertificateStore _secureCertificateStore;

public InspectorBehavior(ISecureCertificateStore secureCertificateStore)
{
// not calling the base implementation
_secureCertificateStore = secureCertificateStore;
}

public void Validate(ServiceEndpoint endpoint)
{
// not calling the base implementation
}

public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
// not calling the base implementation
}

public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
// not calling the base implementation
}

public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add(new ClientInspector(_secureCertificateStore));
}
}

public class ClientInspector : IClientMessageInspector
{
public MessageHeader[] Headers { get; set; }
private ISecureCertificateStore _secureCertificateStore;

public ClientInspector(ISecureCertificateStore secureCertificateStore, params MessageHeader[] headers)
{
_secureCertificateStore = secureCertificateStore;
Headers = headers;
}

public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
MessageBuffer buffer = request.CreateBufferedCopy(int.MaxValue);
Message copy = buffer.CreateMessage();

if (Headers != null)
{
for (int i = Headers.Length - 1; i >= 0; i--)
request.Headers.Insert(0, Headers[i]);
}

SoapEnvelopeUtility.AddSecurityElements(ref copy, _secureCertificateStore);

request = copy;

return null;
}

public void AfterReceiveReply(ref Message reply, object correlationState)
{
// not calling the base implementation
}
}
}
Loading