Imports Leadtools
Imports Leadtools.Ccow
Imports Leadtools.Ccow.UI
<Test> _
Public Sub SecureBinding()
Dim contextManager As IContextManager = Utils.COMCreateObject(Of IContextManager)(CcowProgId)
Dim participant As SecureParticipant = New SecureParticipant()
Dim secure As ISecureBinding = TryCast(contextManager, ISecureBinding)
Dim coupon As Integer = 0
Try
Dim binderPublicKey As String = String.Empty
Dim mac As String = String.Empty, hash As String
Dim access As Object
coupon = contextManager.JoinCommonContext(participant, ApplicationName, True, False)
'
' Bind securely context manager
'
mac = secure.InitializeBinding(coupon, Leadtools.Ccow.Constants.PassCodeNames, Leadtools.Ccow.Constants.PassCodeValues, binderPublicKey)
hash = Utils.BinaryEncode(Utils.Hash(binderPublicKey & SecureParticipant.Passcode))
Debug.Assert(mac.ToLower() = hash.ToLower())
'
' Create participant mac and finalize binding
'
mac = Utils.BinaryEncode(Utils.Hash(participant.PublicKey + SecureParticipant.Passcode))
access = secure.FinalizeBinding(coupon, participant.PublicKey, mac)
'
' Display access
'
If Not access Is Nothing Then
Dim a As String() = CType(access, String())
Dim i As Integer = 0
Do While i < a.Length
Debug.WriteLine(String.Format(" {0}" & Microsoft.VisualBasic.Constants.vbTab & "{1}", a(i), a(i + 1)))
i += 2
Loop
End If
SetUserContext(contextManager, participant, coupon)
contextManager.LeaveCommonContext(coupon)
Catch e As Exception
Debug.WriteLine(e.Message)
End Try
End Sub
Private Sub SetUserContext(ByVal contextManager As IContextManager, ByVal participant As SecureParticipant, ByVal coupon As Integer)
Dim secure As ISecureContextData = TryCast(contextManager, ISecureContextData)
Dim userSubject As Subject = New Subject("User")
Dim transactionCoupon As Integer = 0
Dim noContinue As Boolean = True, disconnect As Boolean = False
Dim reasons As Object
Dim decision As String = "accept"
userSubject.Items.Add(New ContextItem("User.id.logon"))
userSubject.Items(0).Value = "test"
userSubject.Items.Add(New ContextItem("User.co.Name"))
userSubject.Items(1).Value = "Test User"
Try
Dim messageDigest, appSignature As String
Dim values As List(Of String) = New List(Of String)()
For Each v As Object In userSubject.ToItemValueArray()
values.Add(v.ToString())
Next v
transactionCoupon = contextManager.StartContextChanges(coupon)
'
' Create digital signature
'
messageDigest = coupon.ToString() & String.Join("",userSubject.ToItemNameArray()) + String.Join("",values.ToArray()) + transactionCoupon.ToString()
appSignature = participant.CreateSignature(messageDigest)
secure.SetItemValues(coupon,userSubject.ToItemNameArray(),userSubject.ToItemValueArray(), transactionCoupon,appSignature)
reasons = contextManager.EndContextChanges(transactionCoupon,noContinue)
'
' If any application responded that they cannot apply the change we need to display
' a dialog that displays the reasons for the problems.
'
If (Not reasons Is Nothing AndAlso (CType(reasons, String())).Length > 0) OrElse noContinue Then
Dim pd As ProblemDialog = New ProblemDialog(CType(reasons, String()), noContinue)
Dim result As DialogResult
result = pd.ShowDialog()
If noContinue Then
decision = "cancel"
End If
If result = System.Windows.Forms.DialogResult.OK Then
decision = "accept"
ElseIf result = DialogResult.Cancel Then
decision = "cancel"
Else
decision = "cancel"
disconnect = True
End If
End If
'
' If user decided to break context we must leave
'
contextManager.PublishChangesDecision(transactionCoupon,decision)
If disconnect Then
contextManager.LeaveCommonContext(coupon)
End If
Catch e As Exception
Debug.WriteLine(e.Message)
End Try
End Sub
<ComVisible(True)> _
Public Class SecureParticipant
Implements IContextParticipant
Public Const Passcode As String = "A2C053FC-182C-4167-BB56-EE394BC5DB05"
Public Const ApplicationName As String = "LEADTOOLS CCOW App"
Private _KeyContainer As KeyContainer = Nothing
Public ReadOnly Property PublicKey() As String
Get
If Not _KeyContainer Is Nothing Then
Return Utils.BinaryEncode(_KeyContainer.GetPublicKey())
End If
Return String.Empty
End Get
End Property
Public Sub New()
_KeyContainer = New KeyContainer(ApplicationName)
End Sub
Public Function CreateSignature(ByVal messageDigest As String) As String
Dim signature As Byte() = _KeyContainer.Sign(messageDigest)
Return Utils.BinaryEncode(signature)
End Function
#Region "IContextParticipant Members"
Public Sub CommonContextTerminated() Implements IContextParticipant.CommonContextTerminated
Console.WriteLine("CommonContextTerminated")
End Sub
Public Sub ContextChangesAccepted(ByVal contextCoupon As Integer) Implements IContextParticipant.ContextChangesAccepted
Console.WriteLine("ContextChangesAccepted")
End Sub
Public Sub ContextChangesCanceled(ByVal contextCoupon As Integer) Implements IContextParticipant.ContextChangesCanceled
Console.WriteLine("ContextChangesCanceled")
End Sub
Public Function ContextChangesPending(ByVal contextCoupon As Integer, ByRef reason As String) As String Implements IContextParticipant.ContextChangesPending
reason = String.Empty
Console.WriteLine("ContextChangesPending")
Return "accept"
End Function
Public Sub Ping() Implements IContextParticipant.Ping
End Sub
#End Region
End Class
using Leadtools;
using Leadtools.Ccow;
using Leadtools.Ccow.UI;
public void SecureBinding()
{
IContextManager contextManager = Utils.COMCreateObject<IContextManager>(CcowProgId);
SecureParticipant participant = new SecureParticipant();
ISecureBinding secure = contextManager as ISecureBinding;
int coupon = 0;
try
{
string binderPublicKey = string.Empty;
string mac = string.Empty, hash;
object access;
coupon = contextManager.JoinCommonContext(participant, ApplicationName, true, false);
//
// Bind securely context manager
//
mac = secure.InitializeBinding(coupon, Constants.PassCodeNames, Constants.PassCodeValues, ref binderPublicKey);
hash = Utils.BinaryEncode(Utils.Hash(binderPublicKey + SecureParticipant.Passcode));
Debug.Assert(mac.ToLower() == hash.ToLower());
//
// Create participant mac and finalize binding
//
mac = Utils.BinaryEncode(Utils.Hash(participant.PublicKey + SecureParticipant.Passcode));
access = secure.FinalizeBinding(coupon, participant.PublicKey, mac);
//
// Display access
//
if (access != null)
{
string[] a = (string[])access;
for (int i = 0; i < a.Length; i += 2)
{
Debug.WriteLine(string.Format(" {0}\t{1}", a[i], a[i + 1]));
}
}
SetUserContext(contextManager, participant, coupon);
contextManager.LeaveCommonContext(coupon);
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
private void SetUserContext(IContextManager contextManager, SecureParticipant participant, int coupon)
{
ISecureContextData secure = contextManager as ISecureContextData;
Subject userSubject = new Subject("User");
int transactionCoupon = 0;
bool noContinue = true, disconnect = false;
object reasons;
string decision = "accept";
userSubject.Items.Add(new ContextItem("User.id.logon"));
userSubject.Items[0].Value = "test";
userSubject.Items.Add(new ContextItem("User.co.Name"));
userSubject.Items[1].Value = "Test User";
try
{
string messageDigest,appSignature;
List<string> values = new List<string>();
foreach(object v in userSubject.ToItemValueArray())
{
values.Add(v.ToString());
}
transactionCoupon = contextManager.StartContextChanges(coupon);
//
// Create digital signature
//
messageDigest = coupon.ToString() + string.Join("",userSubject.ToItemNameArray()) +
string.Join("",values.ToArray()) + transactionCoupon.ToString();
appSignature = participant.CreateSignature(messageDigest);
secure.SetItemValues(coupon,userSubject.ToItemNameArray(),userSubject.ToItemValueArray(),
transactionCoupon,appSignature);
reasons = contextManager.EndContextChanges(transactionCoupon,ref noContinue);
//
// If any application responded that they cannot apply the change we need to display
// a dialog that displays the reasons for the problems.
//
if ((reasons != null && ((string[])reasons).Length > 0) || noContinue)
{
ProblemDialog pd = new ProblemDialog((string[])reasons, noContinue);
DialogResult result;
result = pd.ShowDialog();
if (noContinue)
decision = "cancel";
if (result == DialogResult.OK)
decision = "accept";
else if (result == DialogResult.Cancel)
decision = "cancel";
else
{
decision = "cancel";
disconnect = true;
}
}
//
// If user decided to break context we must leave
//
contextManager.PublishChangesDecision(transactionCoupon,decision);
if(disconnect)
{
contextManager.LeaveCommonContext(coupon);
}
}
catch(Exception e)
{
Debug.WriteLine(e.Message);
}
}
[ComVisible(true)]
public class SecureParticipant : IContextParticipant
{
public const string Passcode = "A2C053FC-182C-4167-BB56-EE394BC5DB05";
public const string ApplicationName = "LEADTOOLS CCOW App";
private KeyContainer _KeyContainer = null;
public string PublicKey
{
get
{
if (_KeyContainer != null)
return Utils.BinaryEncode(_KeyContainer.GetPublicKey());
return string.Empty;
}
}
public SecureParticipant()
{
_KeyContainer = new KeyContainer(ApplicationName);
}
public string CreateSignature(string messageDigest)
{
byte[] signature = _KeyContainer.Sign(messageDigest);
return Utils.BinaryEncode(signature);
}
#region IContextParticipant Members
public void CommonContextTerminated()
{
Console.WriteLine("CommonContextTerminated");
}
public void ContextChangesAccepted(int contextCoupon)
{
Console.WriteLine("ContextChangesAccepted");
}
public void ContextChangesCanceled(int contextCoupon)
{
Console.WriteLine("ContextChangesCanceled");
}
public string ContextChangesPending(int contextCoupon, ref string reason)
{
reason = string.Empty;
Console.WriteLine("ContextChangesPending");
return "accept";
}
public void Ping()
{
}
#endregion
}