Skip to content

Commit b70c076

Browse files
committed
message level encryption changes for NVP and soap client
1 parent d64ee1a commit b70c076

File tree

8 files changed

+80
-11
lines changed

8 files changed

+80
-11
lines changed

CyberSource/Client/BaseClient.cs

+20-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.ServiceModel;
55
using System.Xml.Serialization;
66
using System.ServiceModel.Channels;
7+
using System.ServiceModel.Security.Tokens;
78

89
namespace CyberSource.Clients
910
{
@@ -40,6 +41,9 @@ public abstract class BaseClient
4041
private const string PROXY_PASSWORD = "proxyPassword";
4142
private const string BASIC_AUTH = "Basic";
4243

44+
public const string CYBERSOURCE_PUBLIC_KEY = "CyberSource_SJC_US";
45+
public const string X509_CLAIMTYPE = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/x500distinguishedname";
46+
4347
static BaseClient()
4448
{
4549
SetupProxy();
@@ -211,6 +215,12 @@ int boolVal
211215
= AppSettings.GetIntSetting(
212216
merchantID, Configuration.CONNECTION_LIMIT);
213217

218+
// Encryption enable flag
219+
boolVal
220+
= AppSettings.GetBoolSetting(
221+
merchantID, Configuration.USE_SIGNED_AND_ENCRYPTED);
222+
if (boolVal != -1) config.UseSignedAndEncrypted = (boolVal == 1);
223+
214224
return (config);
215225
}
216226

@@ -303,7 +313,7 @@ protected static void SetConnectionLimit(Configuration config)
303313
/// Returns a custom wcf binding that will create a SOAP request
304314
/// compatible with the Simple Order API Service
305315
/// </summary>
306-
protected static CustomBinding getWCFCustomBinding()
316+
protected static CustomBinding getWCFCustomBinding(Configuration config)
307317
{
308318
//Setup custom binding with HTTPS + Body Signing
309319
CustomBinding currentBinding = new CustomBinding();
@@ -315,6 +325,15 @@ protected static CustomBinding getWCFCustomBinding()
315325
asec.EnableUnsecuredResponse = true;
316326
asec.SecurityHeaderLayout = SecurityHeaderLayout.Lax;
317327

328+
if (config.UseSignedAndEncrypted)
329+
{
330+
asec.LocalClientSettings.IdentityVerifier = new CustomeIdentityVerifier();
331+
asec.RecipientTokenParameters = new System.ServiceModel.Security.Tokens.X509SecurityTokenParameters { InclusionMode = SecurityTokenInclusionMode.Once };
332+
asec.MessageProtectionOrder = System.ServiceModel.Security.MessageProtectionOrder.SignBeforeEncrypt;
333+
asec.EndpointSupportingTokenParameters.SignedEncrypted.Add(new System.ServiceModel.Security.Tokens.X509SecurityTokenParameters());
334+
asec.SetKeyDerivation(false);
335+
}
336+
318337
//Use custom encoder to strip unsigned timestamp in response
319338
CustomTextMessageBindingElement textBindingElement = new CustomTextMessageBindingElement();
320339

CyberSource/Client/Configuration.cs

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class Configuration
2727
internal const string CONNECTION_LIMIT = "connectionLimit";
2828
internal const string SEND_TO_AKAMAI = "sendToAkamai";
2929
internal const string EFFECTIVE_SERVER_URL = "effectiveServerURL";
30+
internal const string USE_SIGNED_AND_ENCRYPTED = "useSignAndEncrypted";
3031

3132
/// <summary>
3233
/// Default log file name.
@@ -68,6 +69,7 @@ public class Configuration
6869
private bool demo = false;
6970
private bool sendToAkamai = true;
7071
private int connectionLimit = -1;
72+
private bool useSignedAndEncrypted = false;
7173

7274
private bool isSendToProductionSet = false;
7375

@@ -406,5 +408,11 @@ private void CheckMerchantID()
406408
"CONFIGURATION OR CODE BUG: merchantID is missing!");
407409
}
408410
}
411+
412+
public bool UseSignedAndEncrypted
413+
{
414+
get { return useSignedAndEncrypted; }
415+
set { useSignedAndEncrypted = value; }
416+
}
409417
}
410418
}

CyberSource/Client/CyberSourceClients.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<ItemGroup>
9898
<Reference Include="System" />
9999
<Reference Include="System.Configuration" />
100+
<Reference Include="System.IdentityModel" />
100101
<Reference Include="System.Runtime.Serialization" />
101102
<Reference Include="System.Security" />
102103
<Reference Include="System.ServiceModel" />
@@ -106,6 +107,7 @@
106107
<Compile Include="AppSettings.cs" />
107108
<Compile Include="BaseClient.cs" />
108109
<Compile Include="Configuration.cs" />
110+
<Compile Include="CustomeIdentityVerifier.cs" />
109111
<Compile Include="CustomTextMessageEncoder.cs" />
110112
<Compile Include="CustomTextMessageEncoderFactory.cs" />
111113
<Compile Include="CustomTextMessageEncodingBindingElement.cs" />

CyberSource/Client/NVPClient.cs

+20-5
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@ public static Hashtable RunTransaction(
6161
SetConnectionLimit(config);
6262

6363
//Setup custom binding with HTTPS + Body Signing
64-
CustomBinding currentBinding = getWCFCustomBinding();
64+
CustomBinding currentBinding = getWCFCustomBinding(config);
6565

6666
//Setup endpoint Address with dns identity
6767
AddressHeaderCollection headers = new AddressHeaderCollection();
6868
EndpointAddress endpointAddress = new EndpointAddress(new Uri(config.EffectiveServerURL), EndpointIdentity.CreateDnsIdentity(config.EffectivePassword), headers);
6969

7070
//Get instance of service
7171
using (proc = new NVPTransactionProcessorClient(currentBinding, endpointAddress))
72-
{
73-
74-
//Set protection level to sign only
75-
proc.Endpoint.Contract.ProtectionLevel = System.Net.Security.ProtectionLevel.Sign;
72+
{
73+
74+
//Set protection level to sign
75+
proc.Endpoint.Contract.ProtectionLevel = System.Net.Security.ProtectionLevel.Sign;
7676

7777
// set the timeout
7878
TimeSpan timeOut = new TimeSpan(0, 0, 0, config.Timeout, 0);
@@ -96,6 +96,21 @@ public static Hashtable RunTransaction(
9696
proc.ClientCredentials.ServiceCertificate.DefaultCertificate = cert1;
9797
break;
9898
}
99+
}
100+
101+
if (config.UseSignedAndEncrypted)
102+
{
103+
foreach (X509Certificate2 cert2 in collection)
104+
{
105+
//Console.WriteLine(cert1.Subject);
106+
if (cert2.Subject.Contains(CYBERSOURCE_PUBLIC_KEY))
107+
{
108+
//Set protection level to sign & encrypt only
109+
proc.Endpoint.Contract.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
110+
proc.ClientCredentials.ServiceCertificate.DefaultCertificate = cert2;
111+
break;
112+
}
113+
}
99114
}
100115

101116
if (logger != null)

CyberSource/Client/SoapClient.cs

+20-5
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ public static ReplyMessage RunTransaction(
5858
DetermineEffectiveMerchantID(ref config, requestMessage);
5959
SetVersionInformation(requestMessage);
6060
logger = PrepareLog(config);
61-
SetConnectionLimit(config);
62-
63-
64-
CustomBinding currentBinding = getWCFCustomBinding();
61+
SetConnectionLimit(config);
62+
63+
64+
CustomBinding currentBinding = getWCFCustomBinding(config);
6565

6666

6767
//Setup endpoint Address with dns identity
@@ -71,7 +71,7 @@ public static ReplyMessage RunTransaction(
7171
//Get instance of service
7272
using( proc = new TransactionProcessorClient(currentBinding, endpointAddress)){
7373

74-
//Set protection level to sign only
74+
//Set protection level to sign & encrypt only
7575
proc.Endpoint.Contract.ProtectionLevel = System.Net.Security.ProtectionLevel.Sign;
7676

7777
// set the timeout
@@ -96,6 +96,21 @@ public static ReplyMessage RunTransaction(
9696
proc.ClientCredentials.ServiceCertificate.DefaultCertificate = cert1;
9797
break;
9898
}
99+
}
100+
101+
if (config.UseSignedAndEncrypted)
102+
{
103+
foreach (X509Certificate2 cert2 in collection)
104+
{
105+
//Console.WriteLine(cert1.Subject);
106+
if (cert2.Subject.Contains(CYBERSOURCE_PUBLIC_KEY))
107+
{
108+
//Set protection level to sign & encrypt only
109+
proc.Endpoint.Contract.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
110+
proc.ClientCredentials.ServiceCertificate.DefaultCertificate = cert2;
111+
break;
112+
}
113+
}
99114
}
100115

101116
// send request now

CyberSourceSamples/src/nvp/app.config

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
<add key="cybs.enableLog" value="false"/>
2121
<add key="cybs.logDirectory" value="Log Directory"/>
2222

23+
<!-- Below flag control encryption of request body. If set to true then request body
24+
will be both signed and encrypted else only signing will be done -->
25+
<add key="cybs.useSignAndEncrypted" value="false"/>
26+
2327
<!-- DO NOT INCLUDE THIS PROPERTY IN YOUR OWN APPLICATIONS! -->
2428
<add key="cybs.demo" value="true"/>
2529

CyberSourceSamples/src/soap/app.config

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
<!-- an issue. -->
5959
<add key="cybs.enableLog" value="false"/>
6060
<add key="cybs.logDirectory" value="Log directory"/>
61+
62+
<!-- Below flag control encryption of request body. If set to true then request body
63+
will be both signed and encrypted else only signing will be done -->
64+
<add key="cybs.useSignAndEncrypted" value="false"/>
6165

6266
<!-- DO NOT INCLUDE THIS PROPERTY IN YOUR OWN APPLICATIONS! -->
6367
<add key="cybs.demo" value="true"/>

app.config

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<!-- an issue. -->
1111
<add key="cybs.enableLog" value="false"/>
1212
<add key="cybs.logDirectory" value="your_log_dir(baseDir\simapi-c-n.n.n\logs)"/>
13+
14+
<add key="cybs.useSignAndEncrypted" value="false"/>
1315

1416
<!-- Please refer to the Connection Limit section in the README for -->
1517
<!-- details on this setting and alternate ways to set the -->

0 commit comments

Comments
 (0)