/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/
// CredCalc.cpp : implementation file
//

#include "stdafx.h"
#include "scts3devman.h"
#include "CredCalc.h"
#include "SctsAuth.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCredCalc dialog


CCredCalc::CCredCalc(CWnd* pParent /*=NULL*/)
	: CDialog(CCredCalc::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCredCalc)
	m_b64 = _T("");
	m_b64md5 = _T("");
	m_cred = _T("");
	m_md5 = _T("");
	m_nonce = _T("");
	m_password = _T("");
	m_user = _T("");
	m_byte = FALSE;
	m_sync11 = TRUE;
	//}}AFX_DATA_INIT
	oldNonce="";
	ufNonceSize=0;
}


void CCredCalc::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCredCalc)
	DDX_Text(pDX, IDC_CCB64, m_b64);
	DDX_Text(pDX, IDC_CCB64MD5, m_b64md5);
	DDX_Text(pDX, IDC_CCCRED, m_cred);
	DDX_Text(pDX, IDC_CCMD5, m_md5);
	DDX_Text(pDX, IDC_CCNONCE, m_nonce);
	DDX_Text(pDX, IDC_CCPASSWORD, m_password);
	DDX_Text(pDX, IDC_CCUSER, m_user);
	DDX_Check(pDX, IDC_CCBYTE, m_byte);
	DDX_Check(pDX, IDC_CCPROTO, m_sync11);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCredCalc, CDialog)
	//{{AFX_MSG_MAP(CCredCalc)
	ON_EN_CHANGE(IDC_CCUSER, OnChangeCcuser)
	ON_EN_CHANGE(IDC_CCPASSWORD, OnChangeCcpassword)
	ON_EN_CHANGE(IDC_CCNONCE, OnChangeCcnonce)
	ON_EN_CHANGE(IDC_CCB64, OnChangeCcb64)
	ON_EN_UPDATE(IDC_CCNONCE, OnUpdateCcnonce)
	ON_BN_CLICKED(IDC_CCBYTE, OnCcbyte)
	ON_BN_CLICKED(IDC_CCPROTO, OnCcproto)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCredCalc message handlers
void CCredCalc::CalculateCred()
{
	MD5_CTX md5Context;
	char md5Digest[16];
	unsigned int size=0;
	unsigned int tint;
	char *cred;
	char buffer[500]="";
	UpdateData(TRUE);
	m_md5="";
	m_b64="";
	m_b64md5="";
	m_cred="";
	if (m_user!="")
	{
		strcat(buffer,(LPCTSTR)m_user);
		size+=m_user.GetLength();
	}
	if (m_password!="")
	{
		if (size) 
		{
			strcat(buffer,":");
			size++;
		}
		strcat(buffer,(LPCTSTR)m_password);
		size+=m_password.GetLength();
	}
	if (m_sync11)
	{
		MD5Init(&md5Context);
		MD5Update(&md5Context,buffer,size);
		MD5Final(md5Digest,&md5Context);
	}
	if (m_nonce!="")
	{
		if (size) 
		{
			strcat(buffer,":");
			size++;
		}
		memcpy(buffer+size,ufNonce,ufNonceSize);
		size+=ufNonceSize;
	}
	if (!size) 
	{
		UpdateData(FALSE);
		return;
	}
	buffer[size]=0;
	if (m_byte)
	{
		/* Ref#0025 : Don't use the md5digest as a temporary buffer, affects 1.1 */
		int pos;
		char temp[10];
		for (pos=0;pos<size;pos++)
		{
			sprintf(temp,"%02X ",(unsigned char)buffer[pos]);
			m_cred+=temp;
		}
	}
	else
		m_cred=buffer;
	tint=size;
	cred=sctsBase64Encode((unsigned char *)buffer,size);
	m_b64=cred;
	sctsFree(cred);
	if (m_sync11)
	{
		size=16;
		cred=sctsBase64Encode((unsigned char *)md5Digest,size);
		sctsMemcpy(buffer,cred,size);
		sctsMemcpy(buffer+size+1,ufNonce,ufNonceSize);
		buffer[size]=':';
		tint=size+ufNonceSize+1;
		sctsFree(cred);
	}
	MD5Init(&md5Context);
	MD5Update(&md5Context,buffer,tint);
	MD5Final(md5Digest,&md5Context);
	for (size=0;size<16;size++)
	{
		sprintf(buffer,"%02X ",(unsigned char)md5Digest[size]);
		m_md5+=buffer;
	}
	cred=sctsBase64Encode((unsigned char *)md5Digest,size);
	m_b64md5=cred;
	sctsFree(cred);
	UpdateData(FALSE);
}

void CCredCalc::OnChangeCcuser() 
{
	CalculateCred();	
}

void CCredCalc::OnChangeCcpassword() 
{
	CalculateCred();	
}

void CCredCalc::OnChangeCcnonce() 
{
	CalculateCred();	
}

void CCredCalc::OnChangeCcb64() 
{
	MD5_CTX md5Context;
	char md5Digest[16];
	unsigned char *cred;
	char c;
	int pos;
	int i;
	unsigned int size;
	CString byteStream="";
	bool flag=true;
	char buffer[10];
	UpdateData(TRUE);
	size=m_b64.GetLength();
	cred=sctsBase64Decode((char *)(LPCTSTR)m_b64,size);
	m_user="";
	m_password="";
	m_nonce="";
	m_md5="";
	m_b64md5="";
	m_cred="";
	if (!cred)
	{
		UpdateData(FALSE);
		return;
	}
	pos=0;
	for (i=0;i<size;i++)
	{
		if (flag && (cred[i]<32 || cred[i]>126)) flag=false;
		sprintf(buffer,"%02X ",cred[i]);
		byteStream+=buffer;
		m_cred+=buffer;
		if (cred[i]==':')
		{
			if (m_user=="" && flag)
			{
				c=cred[i];
				cred[i]=0;
				m_user=cred;
				cred[i]=c;
				pos=i+1;
				byteStream="";
			}
			else if (m_password=="" && flag)
			{
				c=cred[i];
				cred[i]=0;
				m_password=cred+pos;
				cred[i]=c;
				pos=i+1;
				byteStream="";
			}
		}
	}
	if (flag)
	{
		c=cred[size-1];
		cred[size-1]=0;
		if (pos!=size)
		{
			m_nonce=(cred+pos);
			m_nonce+=c;
		}
		m_cred=cred;
		m_cred+=c;
	}
	else
		m_nonce=byteStream;
	MD5Init(&md5Context);
	MD5Update(&md5Context,(char *)cred,size);
	MD5Final(md5Digest,&md5Context);
	for (size=0;size<16;size++)
	{
		sprintf(buffer,"%02X ",(unsigned char)md5Digest[size]);
		m_md5+=buffer;
	}
	sctsFree(cred);
	cred=(unsigned char *)sctsBase64Encode((unsigned char *)md5Digest,size);
	m_b64md5=cred;
	sctsFree(cred);
	UpdateData(FALSE);
}

void CCredCalc::OnUpdateCcnonce() 
{
	CString newNonce="";
	int pos;
	int cnt;
	bool flag=true;
	char *nonce;
	unsigned char *_ufNonce=ufNonce;
	unsigned char b,c;
	UpdateData(TRUE);
	pos=m_nonce.GetLength();
	nonce=(char *)(LPCTSTR)m_nonce;
	if (!m_byte) 
	{
		ufNonceSize=pos;
		memcpy(ufNonce,nonce,pos);
		return;
	}
	m_nonce.MakeUpper();
	while (pos--)
	{
		c=*nonce++;
		if (c==' ') continue;
		if ((c<'0' || c>'9') && (c<'A' || c>'F'))
		{
			m_nonce=oldNonce;
			UpdateData(FALSE);
			((CEdit *)GetDlgItem(IDC_CCNONCE))->SetSel(m_nonce.GetLength(),m_nonce.GetLength());
			return;
		}
		newNonce+=c;
	}
	pos=newNonce.GetLength();
	nonce=(char *)(LPCTSTR)newNonce;
	m_nonce="";
	ufNonceSize=0;
	for (cnt=0;cnt<pos;++cnt)
	{
		c=*nonce++;
		m_nonce+=c;
		if (cnt&1) 
		{
			if (cnt<pos-1) m_nonce+=' ';
			if (b<'A')
				b-='0';
			else
				b=b+10-'A';
			if (c<'A')
				c-='0';
			else
				c=c+10-'A';
			c=(b<<4)+c;
			*_ufNonce=c;
			if (flag && (c<32 || c>126)) flag=false;
			ufNonceSize++;
			_ufNonce++;
		}
		else
			b=c;
	}
	GetDlgItem(IDC_CCBYTE)->EnableWindow(flag);
	oldNonce=m_nonce;
	UpdateData(FALSE);
	((CEdit *)GetDlgItem(IDC_CCNONCE))->SetSel(m_nonce.GetLength(),m_nonce.GetLength());
}

void CCredCalc::OnCcbyte() 
{
	int pos;
	char buffer[20];
	UpdateData(TRUE);
	if (m_byte)
	{
		m_nonce="";
		for (pos=0;pos<ufNonceSize;pos++)
		{
			sprintf(buffer,"%02X ",ufNonce[pos]);
			m_nonce+=buffer;
		}
	}
	else
	{
		ufNonce[ufNonceSize]=0;
		m_nonce=ufNonce;
	}
	UpdateData(FALSE);
	CalculateCred();
}

void CCredCalc::OnCcproto() 
{
	CalculateCred();	
}

BOOL CCredCalc::PreTranslateMessage(MSG* pMsg) 
{
	/* Disable ESC  and Enter */
	if (pMsg->message==WM_KEYDOWN && (pMsg->wParam==VK_ESCAPE || pMsg->wParam==VK_RETURN)) return TRUE;
	
	return CDialog::PreTranslateMessage(pMsg);
}

void CCredCalc::OnDestroy() 
{
	CDialog::OnDestroy();
	mainPane->credCalc=0;
}
